package com.fastbee.oss.config;

import cn.xuyanwu.spring.file.storage.FileStorageService;
import cn.xuyanwu.spring.file.storage.platform.AliyunOssFileStorage;
import cn.xuyanwu.spring.file.storage.platform.LocalPlusFileStorage;
import cn.xuyanwu.spring.file.storage.platform.QiniuKodoFileStorage;
import cn.xuyanwu.spring.file.storage.platform.TencentCosFileStorage;
import com.fastbee.oss.domain.OssConfig;
import com.fastbee.oss.enums.PlatformType;
import com.fastbee.oss.service.IAutoConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Configuration
@ConditionalOnMissingBean(FileStorageService.class)
public class AutoConfiguration {
    @Autowired
    private IAutoConfigService autoConfigService;

    @Bean("PlusFileStorage")
    public List<LocalPlusFileStorage> localPlusFileStorageList() {
        List<OssConfig> list = autoConfigService.selectOssConfigByPlatformType(PlatformType.LOCAL.getValue());
        if(list.size() == 0){
            log.info("未配置本地存储平台");
            return new ArrayList<>();
        }
        return list.stream().map(local -> {
            log.info("加载存储平台：{}",local.getPlatform());
            LocalPlusFileStorage localFileStorage = new LocalPlusFileStorage();
            localFileStorage.setPlatform(local.getPlatform());
            localFileStorage.setBasePath(local.getBasePath());
            localFileStorage.setDomain(local.getDomain());
            //localFileStorage.setStoragePath(local.getStoragePath());
            return localFileStorage;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    @Bean("aliyunOssFileStorage")
    @ConditionalOnClass(name = "com.aliyun.oss.OSS")
    public List<AliyunOssFileStorage> aliyunOssFileStorageList() {
        List<OssConfig> list = autoConfigService.selectOssConfigByPlatformType(PlatformType.ALIYUN.getValue());
        if(list.size() == 0){
            log.info("未配置阿里云存储平台");
            return new ArrayList<>();
        }
        return list.stream().map(oss -> {
            log.info("加载存储平台：{}",oss.getPlatform());
            AliyunOssFileStorage storage = new AliyunOssFileStorage();
            storage.setPlatform(oss.getPlatform());
            storage.setAccessKey(oss.getAccessKey());
            storage.setSecretKey(oss.getSecretKey());
            storage.setEndPoint(oss.getRegion());
            storage.setBucketName(oss.getBucketName());
            storage.setDomain(oss.getDomain());
            storage.setBasePath(oss.getBasePath());
            return storage;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    @Bean("tencentCosFileStorage")
    @ConditionalOnClass(name = "com.qcloud.cos.COSClient")
    public List<TencentCosFileStorage> tencentCosFileStorageList() {
        List<OssConfig> list = autoConfigService.selectOssConfigByPlatformType(PlatformType.TENCENT.getValue());
        if(list.size() == 0){
            log.info("未配置腾讯云存储平台");
            return new ArrayList<>();
        }
        return list.stream().map(cos -> {
            log.info("加载存储平台：{}",cos.getPlatform());
            TencentCosFileStorage storage = new TencentCosFileStorage();
            storage.setPlatform(cos.getPlatform());
            storage.setSecretId(cos.getAccessKey());
            storage.setSecretKey(cos.getSecretKey());
            storage.setRegion(cos.getRegion());
            storage.setBucketName(cos.getBucketName());
            storage.setDomain(cos.getDomain());
            storage.setBasePath(cos.getBasePath());
            return storage;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    @Bean("qiniuKodoFileStorage")
    @ConditionalOnClass(name = "com.qiniu.storage.UploadManager")
    public List<QiniuKodoFileStorage> qiniuKodoFileStorageList() {
        List<OssConfig> list = autoConfigService.selectOssConfigByPlatformType(PlatformType.QINIU.getValue());
        if(list.size() == 0){
            log.info("未配置七牛云存储平台");
            return new ArrayList<>();
        }
        return list.stream().map(kodo -> {
            log.info("加载存储平台：{}",kodo.getPlatform());
            QiniuKodoFileStorage storage = new QiniuKodoFileStorage();
            storage.setPlatform(kodo.getPlatform());
            storage.setAccessKey(kodo.getAccessKey());
            storage.setSecretKey(kodo.getSecretKey());
            storage.setBucketName(kodo.getBucketName());
            storage.setDomain(kodo.getDomain());
            storage.setBasePath(kodo.getBasePath());
            return storage;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }
}
